package com.taotao.manage.controller;

import com.taotao.common.pojo.PicUploadResult;
import com.taotao.manage.service.PicUploadService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: taotao
 * @Package: com.taotao.manage.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/4/21 22:50
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/4/21 22:50
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>
 * Copyright: Copyright (c) 2018
 */
@Controller
@RequestMapping("pic")
public class PicUploadController {
    @Autowired
    private PicUploadService  picUploadService;

    public static final List<String> extensions = Arrays.asList("gif", "png", "jpg", "jpeg", "bmp");

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ResponseBody
    public PicUploadResult picUploadController(@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
        PicUploadResult result = new PicUploadResult();
        result.setError(1);
        result.setMessage("图片不符合!");

        if (file == null) {
            return result;
        }
        try {
            //图片后缀名是否正确
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            if (!extensions.contains(extension)) {
                //图片验证不合格,返回
                return result;
            }
            //校验内容是否合法
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null) {
                return result;
            }
            //校验通过
            result.setError(0);
            //保存
            String filePath = this.getFilePath(file.getOriginalFilename());
            file.transferTo(new File(filePath));

            //返回url
            String url = StringUtils.replace(StringUtils.substringAfter(filePath, picUploadService.REPOSITORY_PATH), "\\", "/");
            result.setUrl(picUploadService.IMAGE_BASE_URL+url);
            result.setMessage(null);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;

    }

    private String getFilePath(String sourceFileName) {
        String baseFolder = picUploadService.REPOSITORY_PATH + File.separator + "images";
        Date nowDate = new Date();
        // yyyy/MM/dd
        String fileFolder = baseFolder + File.separator + new DateTime(nowDate).toString("yyyy") + File.separator
                + new DateTime(nowDate).toString("MM") + File.separator + new DateTime(nowDate).toString("dd");
        File file = new File(fileFolder);
        if (!file.isDirectory()) {
            // 如果目录不存在，则创建目录
            file.mkdirs();
        }
        // 生成新的文件名
        String fileName = new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS") + RandomUtils.nextInt(100, 9999) + "."
                + StringUtils.substringAfterLast(sourceFileName, ".");
        return fileFolder + File.separator + fileName;
    }

}